
<?php

function dal_savefile_write($userid, $gameid, $content)
{
	$t_conn   = dal_mocool_get_dbconn();
	$content  = mysql_real_escape_string($content);
	$t_sql    = "update mocool_savefile_table set content='$content', update_ts=NOW() where user_id=$userid and game_id=$gameid";
	$t_result = mysql_query($t_sql, $t_conn);
	if (!$t_result) {
		dal_mysql_error($t_conn, $t_sql, __FUNCTION__);
		return false;
	}
	if (mysql_affected_rows($t_conn) == 0) {
		$t_sql    = "insert into mocool_savefile_table values($userid, $gameid, '$content', NOW())";
		$t_result = mysql_query($t_sql, $t_conn);
		if (!$t_result) {
			dal_mysql_error($t_conn, $t_sql, __FUNCTION__);
			return false;
		}
	}
	return true;
}

function dal_savefile_read($userid, $gameid)
{
	$t_conn   = dal_mocool_get_dbconn();
	$t_sql    = 'select content from mocool_savefile_table where user_id=' . $userid;
	$t_result = mysql_query($t_sql, $t_conn);
	if (!$t_result) {
		dal_mysql_error($t_conn, $t_sql, __FUNCTION__);
		return null;
	}
	if (mysql_num_rows($t_result) != 1) {
		return null;
	}
	$row = mysql_fetch_array($t_result);
	return $row['content'];
}

?>
